package com.xquant.pricing.systerm.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.xquant.common.core.domain.Underlying;
import com.xquant.pricing.calc.mapper.InstrumentMapper;
import com.xquant.pricing.common.utils.StringUtils;
import com.xquant.pricing.common.utils.UnderlyingUtils;
import com.xquant.pricing.systerm.service.InstrumentService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: B946
 * @Date: 23/12/2019 10:17
 * @Description:
 */
@Service
@Transactional
public class InstrumentServiceImpl implements InstrumentService {

    @Autowired
    private InstrumentMapper instrumentMapper;


    /**
     * 项目启动时，初始化到缓存
     */
    @PostConstruct
    public void init() {


    }

    @Override
    public List<Underlying> selectUnderlyingDatas(Map<String, Object> params) {
        if (StringUtils.isEmpty(MapUtils.getString(params, "condition"))) {
            params.put("condition", "000");
        }
        String condition = MapUtils.getString(params, "condition");
        List<Underlying> dictDatas = new ArrayList<>();
        if (condition.split(",").length == 3) {
            Underlying underlying = UnderlyingUtils.getCache(condition);
            if (ObjectUtil.isNotNull(underlying)) {
                dictDatas.add(underlying);
            }
 
        } else {
            dictDatas = UnderlyingUtils.getDimCache(condition);
        }
        if (StringUtils.isNotNull(dictDatas) && dictDatas.size() > 0) {
            return dictDatas;
        }
        params.put("start", String.valueOf(0));
        params.put("end", String.valueOf(15));
        List<Underlying> paramDatas = instrumentMapper.selectUnderlyingDatas(params);
        if (StringUtils.isNotNull(paramDatas)) {
            return paramDatas;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getInstruments(Map<String, Object> params) {
        //java去重
        List<Map<String, Object>> collect = instrumentMapper.getInstruments(params).stream().distinct().collect(Collectors.toList());
        return collect;
    }


    @Override
    public List<Map<String, Object>> getAllInstrument() {
        return instrumentMapper.getAllInstrument();
    }

    @Override
    public List<Map<String, Object>> getInstrumentsBySubject(Map<String, Object> params) {
        //java去重
        List<Map<String, Object>> collect = instrumentMapper.getInstrumentsBySubject(params).stream().distinct().collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Map<String, Object>> getInstruments2(Map<String, Object> params) {
        //java去重
        List<Map<String, Object>> collect = instrumentMapper.getInstruments2(params).stream().distinct().collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Map<String, Object>> getIrs(Map<String, Object> params) {

        return instrumentMapper.getIrs(params);
    }

    @Override
    public List<Map<String, Object>> getBondFutures(Map<String, Object> params) {

        return instrumentMapper.getBondFutures(params);
    }

    @Override
    public List<Map<String, Object>> getBondFutureCtds(Map<String, Object> params) {
        return instrumentMapper.getBondFutureCtds(params);
    }

    @Override
    public List<Map<String, Object>> getBonds(Map<String, Object> params) {
        return instrumentMapper.getBonds(params);
    }

    @Override
    public List<Map<String, Object>> getCrmws(Map<String, Object> params) {
        return instrumentMapper.getCrmws(params);
    }

    @Override
    public List<Map<String, Object>> getFx(Map<String, Object> params) {
        return instrumentMapper.getFx(params);
    }
}
